package jsyf.oa.dao.forms;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.oa.entity.forms.ComponentsEntity;
import jsyf.oa.idao.BaseDao;
import jsyf.oa.ientity.IBaseEntity;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by 92852 on 2018/6/22.
 */
@Repository
public class FormsDao extends BaseDao {
    public FormsDao() {
        this.iBaseEntity = new ComponentsEntity();
    }

    @Override
    public IBaseEntity convertJson2Entity(JSONObject json) {
        return JSON.toJavaObject(json, ComponentsEntity.class);
    }

    public List<JSONObject> getComponents() {
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT\n" +
                "ID AS id,\n" +
                "`NAME` AS name,\n" +
                "TYPE AS type,\n" +
                "IMG_URL AS imgUrl\n" +
                "from\n" +
                "t_forms_components");
        return super.queryForJsonList(sql.toString());
    }

       public String getFormsName(String formsName) {
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT\n" +
                "FORMS_NAME\n" +
                "FROM\n" +
                "t_forms_create\n" +
                "WHERE 1=1\n" +
                "AND FORMS_NAME=" + " '" + formsName + "'");
        String result = super.queryForString(sb.toString());
        return result;
    }
    public String getFormsCreateId(String formsName) {
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT\n" +
                "ID\n" +
                "FROM\n" +
                "t_forms_create\n" +
                "WHERE 1=1\n" +
                "AND FORMS_NAME=" + " '" + formsName + "'");
        String result = super.queryForString(sb.toString());
        return result;
    }

    public List<JSONObject> getFormsCreate(String userId) {
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT\n" +
                "t_sys_user.`NAME` AS createUserName,\n" +
                "t_forms_create.ID AS createId,\n" +
                "t_forms_create.CREATE_TIME AS createTime,\n" +
                "t_forms_create.FORMS_NAME AS formsName\n" +
                "FROM\n" +
                "t_forms_create\n" +
                "LEFT JOIN t_sys_user ON t_sys_user.ID=t_forms_create.CREATE_USER_ID\n" +
                "WHERE 1=1\n" +
                "AND t_forms_create.CREATE_USER_ID="+" '"+userId+" '"+"\n");
        sql.append("ORDER BY t_forms_create.CREATE_TIME DESC");
        return super.queryForJsonList(sql.toString());
    }

    public List<JSONObject> getFieldsAndComponents(String mainId) {
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT\n" +
                "t_forms_components.`NAME` AS componentsName,\n" +
                "t_forms_components.TYPE AS componentsType,\n" +
                "t_forms_fields.DESCRIPTION AS description,\n" +
                "t_forms_fields.PROMPT AS prompt,\n" +
                "t_forms_components.ID AS componentsId,\n" +
                "t_forms_fields.ID AS fieldsId\n" +
                "FROM\n" +
                "t_forms_fields\n" +
                "LEFT JOIN t_forms_components ON t_forms_components.ID=t_forms_fields.COMP_ID\n" +
                "WHERE 1=1\n" +
                "AND t_forms_fields.MAIN_ID="+" '"+mainId+" '");
        return super.queryForJsonList(sql.toString());
    }

}
